package com.like.fragment;

import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.like.adapter.VideoCourseReplyAdapter;
import com.like.entity.CourseInfo;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.InfoGetReplyReq;
import com.like.entity.InfoGetReplyResp;
import com.like.hospitalworld.R;
import com.like.hospitalworld.VideoCourseDetailActivity;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.fragment_video_course_detail_reply)
public class VideoCourseDetailReplyFragment extends BaseFragment {

    @ViewInject(R.id.reply_list)
    private PullToRefreshListView mReplyList;

    private VideoCourseReplyAdapter mReplyAdapter;
    private CourseInfo mCourseInfo;
    private InfoService mInfoService;
    private int mCurrentPage = 0;
    private int mPageCount = 15;

    @Override
    protected void onCreateView() {
        super.onCreateView();
        mInfoService = RetrofitUtil.getService(InfoService.class);
        initList();
        mReplyList.getRefreshableView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                EventBus.getDefault().post(new EventWrapper(null, VideoCourseDetailActivity.class, VideoCourseDetailActivity.REQUEST_CLOSE_COMMENT));
                return false;
            }
        });
        getReplyList(false);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            getReplyList(false);
        }
    }

    public void setCourseInfo(CourseInfo courseInfo) {
        this.mCourseInfo = courseInfo;
    }

    private void initList() {
        mReplyList.setMode(PullToRefreshBase.Mode.BOTH);
        mReplyList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage = 0;
                getReplyList(false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage += 1;
                getReplyList(true);
            }
        });
    }

    public void refreshReplyList() {
        mCurrentPage = 0;
        getReplyList(false);
    }

    private void getReplyList(final boolean append) {
        if (m == null)
            return;
        InfoGetReplyReq req = new InfoGetReplyReq(mCourseInfo.id, mCurrentPage, mPageCount);
        Call<GeneralResponse<InfoGetReplyResp>> call = mInfoService.getInfoReplyList(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<InfoGetReplyResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<InfoGetReplyResp>> call, Response<GeneralResponse<InfoGetReplyResp>> response) {
                mReplyList.onRefreshComplete();
                GeneralResponse<InfoGetReplyResp> resp = response.body();
                InfoGetReplyResp infoGetReplyResp = resp.data;
                List<InfoGetReplyResp.InformationComment> comments = infoGetReplyResp.comments;
                if (mReplyAdapter == null) {
                    mReplyAdapter = new VideoCourseReplyAdapter(comments, m);
                    mReplyList.setAdapter(mReplyAdapter);
                } else
                    mReplyAdapter.update(comments, append);
            }

            @Override
            public void onFailure(Call<GeneralResponse<InfoGetReplyResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
